/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AdmonHorarios;

import Usuarios.conexion;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Miguel
 */
public class InterfazTraslape extends javax.swing.JFrame {

    //Horario:
    public Horario calendario = new Horario();
    //Conexion para la Base de Datos
    EntityManagerFactory emf = conexion.getInstancia().getEMF();
    EntityManager em = emf.createEntityManager();
    
    /**
     * Creates new form InterfazTraslape
     */
    public InterfazTraslape(String entidad) {
        initComponents();
        lblEntidad.setText(entidad);
    }
    
    public void llenarHorario(ArrayList<Hora> horas){
        DefaultTableModel temp = (DefaultTableModel) tblHorario.getModel();
        //Limpiar:
        while(temp.getRowCount()>0){
            temp.removeRow(0);   
        }
        //Llenar:
        for (int fila=0; fila<horas.size(); fila++){
            Object nuevo[]= {horas.get(fila).getHoraInicio()
                    +" - "+horas.get(fila).getHoraFin(), //Periodo
                calendario.getPeriodo(fila).getPeriodo(0).getEstado(), //Lunes
                calendario.getPeriodo(fila).getPeriodo(1).getEstado(), //Martes
                calendario.getPeriodo(fila).getPeriodo(2).getEstado(), //Miercoles
                calendario.getPeriodo(fila).getPeriodo(3).getEstado(), //Jueves
                calendario.getPeriodo(fila).getPeriodo(4).getEstado(), //Viernes
                calendario.getPeriodo(fila).getPeriodo(5).getEstado()}; //Sabado
            temp.addRow(nuevo);

        }
    }
    
    public void setDatos(int id, ArrayList<Hora> periodos, boolean aula){
        if (aula){
            for (int i=0; i<periodos.size(); i++){
                EnlaceHorario enlace = new EnlaceHorario();
                for (int j=1; j<=6; j++){
                    String D="";
                    if (j==1){
                        D="Lunes";
                    }
                    if (j==2){
                        D="Martes";
                    }
                    if (j==3){
                        D="Miércoles";
                    }
                    if (j==4){
                        D="Jueves";
                    }
                    if (j==5){
                        D="Viernes";
                    }
                    if (j==6){
                        D="Sábado";
                    }
                    Query qr = em.createQuery("select ha from Horarioaula ha "
                        + "where ha.aulaidAula.idAula = " + id
                        + " and ha.dia = '" + D
                        + "' and ha.anulado = false");
                    List<Entidades.Horarioaula> HA = qr.getResultList();
                    qr = em.createQuery("select apa from Asignacionperiodoaula apa "
                        + "where apa.horarioAulaidHorarioAula.idHorarioAula "
                        + "= " + HA.get(0).getIdHorarioAula()
                        + " and apa.periodoidPeriodo.idPeriodo = " + periodos.get(i).getNumPeriodo());
                    List<Entidades.Asignacionperiodoaula> APA = qr.getResultList();
                    if(APA.size()>0){
                        PeriodoHorario periodo = new PeriodoHorario();
                        periodo.setDisponible(APA.get(0).getDisponible());
                        enlace.setPeriodo((j-1), periodo);
                    }else{
                        PeriodoHorario periodo = new PeriodoHorario();
                        periodo.setEstado("-");
                        enlace.setPeriodo((j-1), periodo);
                    }
                }
                calendario.setPeriodo(enlace);
            }
        }else{
            for (int i=0; i<periodos.size(); i++){
                EnlaceHorario enlace = new EnlaceHorario();
                for (int j=1; j<=6; j++){
                    String D="";
                    if (j==1){
                        D="Lunes";
                    }
                    if (j==2){
                        D="Martes";
                    }
                    if (j==3){
                        D="Miércoles";
                    }
                    if (j==4){
                        D="Jueves";
                    }
                    if (j==5){
                        D="Viernes";
                    }
                    if (j==6){
                        D="Sábado";
                    }
                    Query qr = em.createQuery("select hc from Horariocatedratico hc "
                        + "where hc.catedraticoidCatedratico.numeroCatedratico = " + id
                        + " and hc.dia = '" + D
                        + "' and hc.anulado = false");
                    List<Entidades.Horariocatedratico> HC = qr.getResultList();
                    qr = em.createQuery("select apc from Asignacionperiodocatedratico apc "
                        + "where apc.horarioCatedraticoidHorarioCatedratico.idHorarioCatedratico "
                        + "= " + HC.get(0).getIdHorarioCatedratico()
                        + " and apc.periodoidPeriodo.idPeriodo = " + periodos.get(i).getNumPeriodo());
                    List<Entidades.Asignacionperiodocatedratico> APC = qr.getResultList();
                    if(APC.size()>0){
                        PeriodoHorario periodo = new PeriodoHorario();
                        periodo.setDisponible(APC.get(0).getDisponible());
                        enlace.setPeriodo((j-1), periodo);
                    }else{
                        PeriodoHorario periodo = new PeriodoHorario();
                        periodo.setEstado("-");
                        enlace.setPeriodo((j-1), periodo);
                    }
                }
                calendario.setPeriodo(enlace);
            }
        }
        llenarHorario(periodos);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblHorario = new javax.swing.JTable();
        lblHorario = new javax.swing.JLabel();
        lblEntidad = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblHorario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Hora", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"
            }
        ));
        jScrollPane1.setViewportView(tblHorario);

        lblHorario.setText("Horario de:");

        lblEntidad.setText("Ing./Aula");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblHorario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblEntidad)
                        .addGap(0, 770, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(420, 420, 420)
                .addComponent(btnSalir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHorario)
                    .addComponent(lblEntidad))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSalir)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(InterfazTraslape.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(InterfazTraslape.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(InterfazTraslape.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(InterfazTraslape.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazTraslape("").setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalir;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblEntidad;
    private javax.swing.JLabel lblHorario;
    private javax.swing.JTable tblHorario;
    // End of variables declaration//GEN-END:variables
}
